import { BasicColumn, FormSchema } from '@/components/Table';
import { h } from 'vue';
import { Switch } from 'ant-design-vue';
import { setRoleStatus } from '@/api/demo/system';
import { useMessage } from '@/hooks/web/useMessage';
import { getAllRoleList } from '@/api/demo/system';
import { uploadApi } from '@/api/sys/upload';

type CheckedType = boolean | string | number;
export const columns: BasicColumn[] = [
  {
    title: '友链图片',
    dataIndex: 'img',
    width: 200,
    align: 'left',
    ifShow: false
  },
  {
    title: '友链内容',
    dataIndex: 'roleName',
    align: 'left',
    width:200
  },
  {
    title: '友链分组Tag',
    dataIndex: 'tag',
    width: 120,
    align: 'left'
  },
  {
    title: '友链地址',
    dataIndex: 'roleValue',
    align: 'left'
  },
  {
    title: '排序',
    dataIndex: 'orderNo',
    width: 80,
    sorter: true,
  },
  {
    title: '开始时间',
    dataIndex: 'orderNo',
    width: 160,
    sorter: true,
    customRender: ({record}) => {
      return record.createTime;
    }
  },
  {
    title: '结束时间',
    dataIndex: 'orderNo',
    width: 160,
    sorter: true,
    customRender: ({record}) => {
      return record.createTime;
    }
  },
  {
    title: '状态',
    dataIndex: 'status',
    width: 120,
    sorter: true,
    customRender: ({ record }) => {
      if (!Reflect.has(record, 'pendingStatus')) {
        record.pendingStatus = false;
      }
      return h(Switch, {
        checked: record.status === '1',
        checkedChildren: '停用',
        unCheckedChildren: '启用',
        loading: record.pendingStatus,
        onChange(checked: CheckedType) {
          record.pendingStatus = true;
          const newStatus = checked ? '1' : '0';
          const { createMessage } = useMessage();
          setRoleStatus(record.id, newStatus)
            .then(() => {
              record.status = newStatus;
              createMessage.success(`已成功修改角色状态`);
            })
            .catch(() => {
              createMessage.error('修改角色状态失败');
            })
            .finally(() => {
              record.pendingStatus = false;
            });
        },
      });
    },
  },
  {
    title: '创建时间',
    dataIndex: 'id',
    width: 180,
    sorter: true,
    fixed: 'right',
    customRender: ({record})=>{
      return record.createTime;
    }
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'roleNme',
    label: '友链名称',
    component: 'Input',
    colProps: { xs: 24, xl: 12, lg: 8, xxl: 6 },
  },
  {
    field: 'url',
    label: '友链地址',
    component: 'Input',
    colProps: { xs: 24, xl: 12, lg: 8, xxl: 6 },
  },
];

export const formSchema: FormSchema[] = [
  {
    label: '友链分组Tag',
    field: 'role',
    component: 'ApiSelect',
    componentProps: {
      api: getAllRoleList,
      labelField: 'roleName',
      valueField: 'roleValue',
    },
    required: true,
  },
  {
    field: 'roleName',
    label: '友链名称',
    required: true,
    component: 'Input',
  },
  {
    field: 'url',
    label: '友链地址',
    required: true,
    component: 'Input',
  },
  {
    field: 'target',
    component: 'Select',
    label: '打开方式',
    defaultValue: '_blank',
    componentProps: {
      options: [
        {
          label: '新窗口',
          value: '_blank',
        },
        {
          label: '当前窗口',
          value: '_self',
        },
      ],
    },
    rules: [{ required: true, message: '覆盖默认生成的校验信息' }],
  },
  {
    field: 'field23',
    component: 'ImageUpload',
    label: '上传图片',
    required: true,
    defaultValue: [
      'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
    ],
    componentProps: {
      api: uploadApi,
      accept: ['png', 'jpeg', 'jpg'],
      maxSize: 2,
      maxNumber: 1,
    },
    // rules: [
    //   {
    //     required: true,
    //     trigger: 'change',
    //     validator(_, value) {
    //       if (isArray(value) && value.length > 0) {
    //         return Promise.resolve();
    //       } else {
    //         return Promise.reject('请选择上传图片');
    //       }
    //     },
    //   },
    // ],
  },
  {
    field: 'status',
    label: '状态',
    component: 'RadioButtonGroup',
    defaultValue: '1',
    componentProps: {
      options: [
        { label: '启用', value: '1' },
        { label: '停用', value: '0' },
      ],
    },
  },
  {
    label: '备注',
    field: 'remark',
    component: 'InputTextArea',
  },
  {
    label: ' ',
    field: 'menu',
    slot: 'menu',
  },
];
